package com.company;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class Test {
    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>();
        list.add(11);
        list.add(22);
        list.add(33);
        list.add(44);
        list.add(99);
        list.add(22);
        //for each 遍历
        for (int e:list) {
            System.out.print(e+" ");//11 22 33 44 99 22
        }
        System.out.println();

        //for 循环遍历
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+" ");//11 22 33 44 99 22
        }
        System.out.println();

        //迭代器遍历(正向遍历)
        ListIterator<Integer> it = list.listIterator();
        while (it.hasNext()){
            System.out.print(it.next()+" ");//11 22 33 44 99 22
        }
        System.out.println();

        //迭代器遍历(方向遍历)
        ListIterator<Integer> it1 = list.listIterator(list.size());
        while (it1.hasPrevious()){
            System.out.print(it1.previous()+" ");//22 99 44 33 22 11
        }
        System.out.println();
    }
    public static void main4(String[] args) {
        LinkedList<Integer> list = new LinkedList<>();
        list.add(11);
        list.add(22);
        list.add(33);
        list.add(44);
        list.add(99);
        list.add(22);
        System.out.print(list+" ");//[11, 22, 33, 44, 99, 22]

        list.add(0,88);//指定位置插入
        System.out.println(list);//[88, 11, 22, 33, 44, 99, 22]

        list.remove(0);//删除指定下标元素
        list.removeFirst();//删除第一个元素
        list.removeLast();//删除最后一个元素
        System.out.println(list);//[22, 33, 44, 99]

        list.set(3,00);//将下标为3的元素设置为0
        System.out.println(list);//[22, 33, 44, 0]

        List<Integer> list1 = list.subList(0,4);//截取list 从[0,4)下标 并复制为新的链表
        System.out.println(list1);//[22, 33, 44, 0]

        list.clear();//清空链表
        System.out.println(list);//[]
    }
    public static void main3(String[] args) {
        List<Integer> list2 = new java.util.ArrayList<>();
        list2.add(22);
        list2.add(122);
        list2.add(233);
        list2.add(344);
        System.out.println(list2);
        List<Integer> list3 = new LinkedList<>(list2);
    }
    public static void main2(String[] args) {
        List<Integer> list1 = new LinkedList<>();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        System.out.println(list1);
    }
    public static void main1(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addFirst(12);//头插法
        myLinkedList.addLast(22);//尾插法
        myLinkedList.addFirst(45);
        myLinkedList.addFirst(22);
        myLinkedList.display();//22 45 12 22

        myLinkedList.addIndex(2,44);//任意位置插入
        myLinkedList.display();//22 45 44 12 22

        myLinkedList.remove(45);//删除元素 45
        myLinkedList.display();//22 44 12 22

        myLinkedList.removeAllKey(22);//删除所有元素 22
        myLinkedList.display();//44 12
    }
}
